﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;

namespace WpfToolkit
{
    internal sealed class CheckGroupTemlateSelector : DataTemplateSelector
    {
        private string _groupNmae;
        private string _displayMemberPath;
        private string _groupType;
        private bool _isThreeState;
        private DataTemplate _checkTemplate;
        private DataTemplate _radioTemplate;
        public CheckGroupTemlateSelector(string displayMemberPath, string groupType, bool isThreeState, string GroupName)
        {
            //Debug.Assert(!(String.IsNullOrEmpty(displayMemberPath) && String.IsNullOrEmpty(groupType)));
            _groupNmae = GroupName;
            _displayMemberPath = displayMemberPath;
            _groupType = groupType;
            _isThreeState = isThreeState;
        }

        public override DataTemplate SelectTemplate(object item, DependencyObject container)
        {
            if (_checkTemplate == null)
            {
                _checkTemplate = new DataTemplate();
                FrameworkElementFactory cb = new FrameworkElementFactory(typeof(CheckBox));
                cb.SetValue(CheckBox.ClickModeProperty, ClickMode.Press);
                cb.SetValue(CheckBox.PaddingProperty, new Thickness(15, 0, 15, 0));
                cb.SetValue(CheckBox.MarginProperty, new Thickness(5));
                cb.SetValue(CheckBox.HeightProperty, (double)24);

                var tagBinding = new Binding
                {
                    Path = new PropertyPath(".")
                };
                cb.SetBinding(CheckBox.TagProperty, tagBinding);

                var binding = new Binding
                {
                    Path = new PropertyPath(_displayMemberPath)
                };
                cb.SetBinding(CheckBox.ContentProperty, binding);

                _checkTemplate.VisualTree = cb;
                _checkTemplate.Seal();
            }

            if (_radioTemplate == null)
            {
                _radioTemplate = new DataTemplate();
                if (_isThreeState)
                {
                    FrameworkElementFactory rb = new FrameworkElementFactory(typeof(ThreeStateRadioButton));
                    rb.SetValue(RadioButton.ClickModeProperty, ClickMode.Press);
                    rb.SetValue(RadioButton.PaddingProperty, new Thickness(15, 0, 15, 0));
                    rb.SetValue(RadioButton.MarginProperty, new Thickness(5));
                    rb.SetValue(RadioButton.HeightProperty, (double)24);
                    rb.SetValue(RadioButton.GroupNameProperty, _groupNmae);

                    var tagBinding = new Binding
                    {
                        Path = new PropertyPath(".")
                    };
                    rb.SetBinding(CheckBox.TagProperty, tagBinding);

                    var binding = new Binding
                    {
                        Path = new PropertyPath(_displayMemberPath)
                    };
                    rb.SetBinding(CheckBox.ContentProperty, binding);

                    _radioTemplate.VisualTree = rb;
                }
                else
                {
                    FrameworkElementFactory rb = new FrameworkElementFactory(typeof(RadioButton));
                    rb.SetValue(RadioButton.ClickModeProperty, ClickMode.Press);
                    rb.SetValue(RadioButton.PaddingProperty, new Thickness(15, 0, 15, 0));
                    rb.SetValue(RadioButton.MarginProperty, new Thickness(5));
                    rb.SetValue(RadioButton.HeightProperty, (double)24);
                    rb.SetValue(RadioButton.GroupNameProperty, _groupNmae);

                    var tagBinding = new Binding
                    {
                        Path = new PropertyPath(".")
                    };
                    rb.SetBinding(CheckBox.TagProperty, tagBinding);

                    var binding = new Binding
                    {
                        Path = new PropertyPath(_displayMemberPath)
                    };
                    rb.SetBinding(CheckBox.ContentProperty, binding);

                    _radioTemplate.VisualTree = rb;
                }
                _radioTemplate.Seal();
            }

            return _groupType.Equals("radio", StringComparison.OrdinalIgnoreCase) ? _radioTemplate : _checkTemplate;
        }

    }
}
